//
//    Copyright (C) 2012  OinkStudio
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package com.marakana.obolo;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;

public class OptionsActivity extends PreferenceActivity {
	private EditTextPreference userName;
	private EditTextPreference rivalName;
	private ListPreference rules;
	private ListPreference numPlayers;
	private ListPreference boardSize;
	private ListPreference dificulty;

	private SharedPreferences prefs;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// s'han de carregar les preferencias guardades.
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.prefs);

		// Afefegeix backgraound a la Activity
		getListView().setBackgroundDrawable(
				getResources().getDrawable(R.drawable.tomboptions));

		// Inicialitza les referencias a les preferencias del prefs.xml
		userName = (EditTextPreference) findPreference("userName");
		boardSize = (ListPreference) findPreference("boardSize");
		rules = (ListPreference) findPreference("rules");
		numPlayers = (ListPreference) findPreference("numPlayers");
		rivalName = (EditTextPreference) findPreference("rivalName");
		dificulty = (ListPreference) findPreference("dificulty");

		// Instancia on es guarden les preferencias
		prefs = userName.getSharedPreferences();

		// Carga el titul de les Preferencias inicials
		userName.setTitle(prefs.getString("userName", "Guest"));
		boardSize.setTitle(prefs.getString("boardSize", "3x3"));
		rules.setTitle(prefs.getString("rules", ""));

		if (prefs.getString("numPlayers", "User").equals("2")) {
			numPlayers.setTitle(prefs.getString("numPlayers", "2")
					+ " Jugadors");
			rivalName.setEnabled(true);
			dificulty.setEnabled(false);
		} else if (prefs.getString("numPlayers", "User").equals("3")) {
			numPlayers.setTitle("IA vs IA");
			rivalName.setEnabled(false);
			dificulty.setEnabled(false);
		} else {
			numPlayers
					.setTitle(prefs.getString("numPlayers", "1") + " Jugador");
			rivalName.setEnabled(false);
			dificulty.setEnabled(true);
		}
		rivalName.setTitle(prefs.getString("rivalName", "Android"));

		if (prefs.getString("dificulty", "Normal").equals("1"))
			dificulty.setTitle("Normal");
		else
			dificulty.setTitle("Dif�cil");

		// Afegeix els tituls dels Dialogs de les preferencias
		userName.setDialogTitle(userName.getSummary());
		boardSize.setDialogTitle(boardSize.getSummary());
		rules.setDialogTitle(rules.getSummary());
		numPlayers.setDialogTitle(numPlayers.getSummary());
		rivalName.setDialogTitle(rivalName.getSummary());

		// Modifica els tituls quan hi ha un canvi de preferencias
		prefs
				.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {
					@Override
					public void onSharedPreferenceChanged(
							SharedPreferences sharedPreferences, String key) {

						if (key.equals("userName")) {
							userName.setTitle(sharedPreferences.getString(key,
									"Guest"));

						} else if (key.equals("boardSize")) {
							boardSize.setTitle(sharedPreferences.getString(key,
									"3x3"));

						} else if (key.equals("rules")) {
							rules.setTitle(sharedPreferences.getString(key,
									"Alta"));

						} else if (key.equals("numPlayers")) {
							if (sharedPreferences.getString(key, "2").equals(
									"2")) {
								numPlayers.setTitle(sharedPreferences
										.getString(key, "2")
										+ " Jugadors");
								rivalName.setEnabled(true);
								dificulty.setEnabled(false);
							} else if (sharedPreferences.getString(key, "3")
									.equals("3")) {
								numPlayers.setTitle("IA vs IA");
								rivalName.setEnabled(false);
								dificulty.setEnabled(false);
							} else {
								numPlayers.setTitle(sharedPreferences
										.getString(key, "1")
										+ " Jugador");
								rivalName.setEnabled(false);
								dificulty.setEnabled(true);
							}

						} else if (key.equals("rivalName")) {
							if (rivalName.isEnabled())
								rivalName.setTitle(sharedPreferences.getString(
										key, "Android"));

						} else if (key.equals("dificulty")) {
							if (sharedPreferences.getString(key, "1").equals(
									"1"))
								dificulty.setTitle("Normal");
							else
								dificulty.setTitle("Dif�cil");
						}
					}
				});
	}

}
